package copyengine.ui.component
{
	import copyengine.ui.CESprite;
	import copyengine.ui.CESpriteEvent;
	import copyengine.utils.GeneralUtils;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class CEUISprite extends CESprite
	{
		private var target:DisplayObjectContainer;
		private var targetBitmap:Bitmap;

		public function CEUISprite(_target:DisplayObjectContainer = null)
		{
			super();
			_target == null ? target = this : target = _target;
			GeneralUtils.addTargetEventListener(target, CESpriteEvent.DISPOSE, onReceiveDisposeEvent);
		}


		private function onReceiveDisposeEvent(e:Event):void
		{
			GeneralUtils.removeTargetEventListener(target, CESpriteEvent.DISPOSE, onReceiveDisposeEvent);
			GeneralUtils.removeTargetFromParent(this);
			target = null;
		}

		//=============
		//==Alpha
		//=============
		override final public function set alpha(value:Number):void
		{
			target == this ? super.alpha = value : target.alpha = value;
		}

		override final public function get alpha():Number
		{
			return target == this ? super.alpha : target.alpha;
		}

		//=============
		//==Visable
		//=============
		override final public function set visible(value:Boolean):void
		{
			target == this ? super.visible = value : target.visible = value
		}

		override final public function get visible():Boolean
		{
			return target == this ? super.visible : target.visible;
		}

		//================
		//==MouseEnable
		//================
		override final public function set mouseEnabled(enabled:Boolean):void
		{
			target == this ? super.mouseEnabled = enabled : target.mouseEnabled = enabled;
		}

		override final public function get mouseEnabled():Boolean
		{
			return target == this ? super.mouseEnabled : target.mouseEnabled;
		}

		//===============
		//==MouseChild
		//===============
		override final public function set mouseChildren(enable:Boolean):void
		{
			target == this ? super.mouseChildren = enable : target.mouseChildren = enable;
		}

		override final public function get mouseChildren():Boolean
		{
			return target == this ? super.mouseChildren : target.mouseChildren;
		}

		//===============
		//==Mask
		//===============
		override final public function set mask(value:DisplayObject):void
		{
			target == this ? super.mask = value : target.mask = value;
		}

		override final public function get mask():DisplayObject
		{
			return target == this ? super.mask : target.mask;
		}

		//==============
		//==MouseX,Y
		//==============
		override final public function get mouseX():Number
		{
			return target == this ? super.mouseX : target.mouseX;
		}

		override final public function get mouseY():Number
		{
			return target == this ? super.mouseY : target.mouseY;
		}

		//===============
		//==Rotation
		//===============
		override final public function set rotation(value:Number):void
		{
			target == this ? super.rotation = value : target.rotation = value;
		}

		override final public function get rotation():Number
		{
			return target == this ? super.rotation : target.rotation;
		}

		//==============
		//==ScrollRect
		//==============
		override final public function set scrollRect(value:Rectangle):void
		{
			target == this ? super.scrollRect = value : target.scrollRect = value;
		}

		override final public function get scrollRect():Rectangle
		{
			return target == this ? super.scrollRect : target.scrollRect;
		}

		//=============
		//==HitTest
		//=============
		override final public function hitTestPoint(x:Number, y:Number, shapeFlag:Boolean = false):Boolean
		{
			return target == this ? super.hitTestPoint(x, y, shapeFlag) : target.hitTestPoint(x, y, shapeFlag);
		}

		//===============
		//==Scale
		//===============

		override final public function set scaleX(value:Number):void
		{
			target == this ? super.scaleX = value : target.scaleX = value;
		}

		override final public function get scaleX():Number
		{
			return target == this ? super.scaleX : target.scaleX
		}

		override final public function set scaleY(value:Number):void
		{
			target == this ? super.scaleY = value : target.scaleY = value;
		}

		override final public function get scaleY():Number
		{
			return target == this ? super.scaleY : target.scaleY;
		}

		override final public function set scaleZ(value:Number):void
		{
			target == this ? super.scaleZ = value : target.scaleZ = value;
		}

		override final public function get scaleZ():Number
		{
			return target == this ? super.scaleZ : target.scaleZ;
		}

		//============
		//==filters
		//============

		override final public function set filters(value:Array):void
		{
			target == this ? super.filters = value : target.filters = value;
		}

		override final public function get filters():Array
		{
			return target == this ? super.filters : target.filters;
		}

		//=============
		//== Bound
		//=============

		override final public function getBounds(targetCoordinateSpace:DisplayObject):Rectangle
		{
			return target == this ? super.getRect(targetCoordinateSpace) : target.getBounds(targetCoordinateSpace);
		}

		override final public function getRect(targetCoordinateSpace:DisplayObject):Rectangle
		{
			return target == this ? super.getRect(targetCoordinateSpace) : target.getRect(targetCoordinateSpace);
		}

		//==============
		//==Event
		//==============
		override final public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
		{
			if (target)
			{
				target == this ? super.addEventListener(type, listener, useCapture, priority, useWeakReference) : target.addEventListener(type, listener, useCapture, priority, useWeakReference);
			}
		}

		override final public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
		{
			if (target)
			{
				target == this ? super.removeEventListener(type, listener, useCapture) : target.removeEventListener(type, listener, useCapture);
			}
		}

		override final public function hasEventListener(type:String):Boolean
		{
			return target == this ? super.hasEventListener(type) : target.hasEventListener(type);
		}

		override final public function dispatchEvent(event:Event):Boolean
		{
			return target == this ? super.dispatchEvent(event) : target.dispatchEvent(event);
		}

		//================
		//== Width
		//===============
		override final public function set width(value:Number):void
		{
			target == this ? super.width = value : target.width = value;
		}

		override final public function get width():Number
		{
			return target == this ? super.width : target.width;
		}

		//================
		//==Height
		//===============
		override final public function set height(value:Number):void
		{
			target == this ? super.height = value : target.height = value;
		}

		override final public function get height():Number
		{
			return target == this ? super.height : target.height;
		}

		//===============
		//==Stage
		//===============
		override final public function get stage():Stage
		{
			return target == this ? super.stage : target.stage;
		}

		//===============
		//==Name
		//===============
		override final public function get name():String
		{
			return target == this ? super.name : target.name;
		}

		override final public function set name(value:String):void
		{
			target == this ? super.name = value : target.name = value;
		}

		//=================
		//== x ,y,z
		//================

		override final public function set x(value:Number):void
		{
			target == this ? super.x = value : target.x = value;
		}

		override final public function get x():Number
		{
			return target == this ? super.x : target.x;
		}

		override final public function set y(value:Number):void
		{
			target == this ? super.y = value : target.y = value;
		}

		override final public function get y():Number
		{
			return target == this ? super.y : target.y;
		}

		override final public function set z(value:Number):void
		{
			target == this ? super.z = value : target.z = value;
		}

		override final public function get z():Number
		{
			return target == this ? super.z : target.z;
		}

		//=============
		//==HitTest
		//=============

		override final public function hitTestObject(obj:DisplayObject):Boolean
		{
			return target == this ? super.hitTestObject(obj) : target.hitTestObject(obj);
		}


	}
}
